var currentUser = "group";
var currentWindow = "list";
/*****************点击聊天列表*********************/
$(document).on("click", ".list_member", function () {
    $(this).addClass("focus_click");
    $(this).siblings().removeClass("focus_click");

    $.hideBubble($(this));
    currentUser = $(this).attr("id"); //绑定聊天窗口
    if (currentWindow == "chat") {
        windowList[currentUser + "Count"] = 0;
        $(".content_body").html("");
        $.showChatLog(windowList[currentUser]);
    } else {
        $(".workspace_body").fadeOut();
        $.showUserInfo(currentUser);
    }
});

$.showChatLog = function (chatLog) {
    $.each(chatLog, function (index, data) {
        var type;
        if (data.userAccount == gUserInfo.userAccount)
            type = "right";
        else type = "left";
        $.sendMsg(data.userIcon, data.content, type)
    })
};

$.hideBubble = function (event) {
    event.find(".message_count").html("");
    event.find(".message_bubble").hide();
};

/*****************模拟左侧列表hover事件*********************/
$(document).on("mouseover mouseout", ".list_member", function (event) {
    var $this = $(this),
        $message_bubble = $this.find(".message_bubble"),
        $close_btn = $this.find(".close_btn");
    if (event.type == "mouseover") {
        $this.addClass("focus");
        $message_bubble.hide();
        $close_btn.show();
    } else if (event.type == "mouseout") {
        $this.removeClass("focus");
        if ($this.find(".message_count").text() != "")
            $message_bubble.show();
        $close_btn.hide();

    }
});

/*****************导航栏实现*********************/
$(document).on("click", ".chat_window", function () {
    currentWindow = "chat";
    $(this).removeClass("new");
    $(this).find(".active_horn").show();
    $(".list_window").find(".active_horn").hide();
    $.showChatWindow();
    $(".content_body").html("");
    $.showChatLog(windowList["group"]);

});
$(document).on("click", ".list_window", function () {
    currentWindow = "list";
    $('.windows_list').html("");
    $.getFriendList(gUserInfo.userAccount);
    $(this).find(".active_horn").show();
    $(".chat_window").find(".active_horn").hide();
    $.showUserInfoWindow();
});


/*****************提醒框实现*********************/
var superTime;
$(document).on("mouseover mouseout ", ".okBtn", function (event) {
    var $notice = $(".ok_notice");
    if (event.type == "mouseover") {
        superTime = setTimeout(function () {
            $notice.addClass("active");
        }, 500);
    } else if (event.type == "mouseout") {
        clearTimeout(superTime);
        $notice.removeClass("active");
    }
});

$(document).on("mouseover mouseout ", "#userIcon", function (event) {
    var $notice = $(".icon_notice");
    if (event.type == "mouseover") {
        superTime = setTimeout(function () {
            $notice.fadeIn();
        }, 500);
    } else if (event.type == "mouseout") {
        clearTimeout(superTime);
        $notice.fadeOut();
    }
});


$(document).on('click', '.chat_btn', function () {
    $(".chat_window").removeClass("new");
    var json = {}, isRepeat = true;
    json.userAccount = $("#account").html();
    json.userIcon = $(".info_head").css("background-color");
    json.userNickname = $("#nickname").html();
    $(".windows_list").html("<li class=\"list_member\" id=\"group\">\n" +
        "<span class=\"member_icon\"></span>\n" +
        "<p class=\"member_nickname\">随便聊</p>\n" +
        "<span class=\"message_bubble\" style=\"display: none;\">" +
        "<p class=\"message_count\"></p></span>" +
        "<span class=\"close_btn\" style=\"display: none;\"><p>×</p></span></li>");
    $.showChatList(json, true);
    if (windowList.chatWindow.length == 0)
        windowList.chatWindow.push(json);
    else {
        isRepeat = false;
        $.each(windowList.chatWindow, function (index, val) {
            if (val.userAccount == json.userAccount) {
                isRepeat = true;
            } else
                $.showChatList(val, false);
        });
    }
    if (!isRepeat)
        windowList.chatWindow.push(json);
    currentWindow = "chat";//窗口状态改变
    currentUser = json.userAccount;
    $(".list_window>.active_horn").fadeOut();
    $(".chat_window>.active_horn").fadeIn();
    $.fadeInChatList();
    $.showChatLog(windowList[currentUser]);
});


$.alert = function (content, type) {
    var $bubble = $(".notice_bubble");
    $bubble.html(content);
    if (type == 1) {
        $bubble.css("background-color", "#2BA245")
    } else {
        $bubble.css("background-color", "#d50f25")
    }
    $bubble.fadeIn();
    setTimeout(function () {
        $bubble.fadeOut();
    }, 2000)
};


$.getFriendList = function (userAccount) {
    $.ajax({
        url: "../ws_friendList/getFriendList",
        type: "post",
        dataType: "json",
        data: {userAccount: userAccount},
        success: function (msg) {
            if (msg) {
                if (msg.success == true) {
                    windowList.friendList = msg.jsonData.sort(function (a, b) {
                        return b.isOnline - a.isOnline
                    });
                    $.each(windowList.friendList, function (index, userDto) {
                        $.showFriendList(userDto);
                    });
                }
            }
        }
    })
};

$.showUserInfo = function (userAccount) {
    $.ajax({
        url: "../ws_socket/getUserInfo",
        type: "post",
        dataType: "json",
        data: {userAccount: userAccount},
        success: function (msg) {
            if (msg) {
                if (msg.success == true) {
                    var json = msg.jsonData;
                    $(".info_head").css("background-color", "rgb(" + json.userIcon.replace(/\../g, "") + ")");
                    $("#account").html(json.userAccount);
                    $("#nickname").html(json.userNickname);
                    if ($.isNull(json.userSex))
                        var userSex = "男";
                    else
                        json.userSex == 0 ? userSex = "男" : userSex = "女";
                    $("#sex").html(userSex);
                    $("#age").html(json.userAge);
                    $("#profile").html(json.userProfile);
                    $(".info_window").fadeIn();
                    $.showUserInfoWindow();

                }
            }
        }
    })
};

$.showUserInfoWindow = function () {
    $(".workspace_body").fadeOut();
    $('.user_info').fadeIn();
};

$.showChatWindow = function () {
    $(".windows_list").html("<li class=\"list_member focus_click\"  id=\"group\">\n" +
        "<span class=\"member_icon\"></span>\n" +
        "<p class=\"member_nickname\">随便聊</p>\n" +
        "<span class=\"message_bubble\" style=\"display: none;\">" +
        "<p class=\"message_count\"></p></span>" +
        "<span class=\"close_btn\" style=\"display: none;\"><p>×</p></span></li>");
    $.each(windowList.chatWindow, function (index, val) {
        $.showChatList(val, false);
    });
    $('.user_info').fadeOut();
    $(".info_window").fadeOut();
    $(".workspace_body").fadeIn();
};

$.fadeInChatList = function () {
    $('.user_info').fadeOut();
    $(".info_window").fadeOut();
    $(".content_body").html("");
    $(".workspace_body").fadeIn();
};

$.isNull = function (data) {
    if (data == "" || !data)
        return true;
    else return false;
};

